let addResourceDialog = {
  label: '新增',
  type: 'button',
  actionType: 'dialog',
  level: 'success',
  dialog: {
    title: '新增资源',
    size: 'md',
    body: {
      type: 'form',
      reload: 'resource_tree',
      api: {
        method: 'post',
        url: '/_dev/resource/add',
        data: {
          '&': '$$',
          system_id: '${system_id}',
          module_id: '${module_id}',
        },
      },
      body: [
        {
          type: 'select',
          name: 'system_id',
          label: '系统名称',
          clearValueOnEmpty: true,
          required: true,
          source: {
            method: 'get',
            url: '/_dev/system/select',
          },
        },
        {
          type: 'select',
          name: 'module_id',
          label: '模块名称',
          clearValueOnEmpty: true,
          required: true,
          source: {
            method: 'get',
            url: '/_dev/module/select?system_id=${system_id}',
            sendOn: '${system_id != null}',
          },
        },
        {
          type: 'input-text',
          name: 'res_code',
          maxLength: 15,
          label: '资源编号',
          clearValueOnEmpty: true,
          required: true,
        },
        {
          type: 'input-text',
          name: 'res_name',
          maxLength: 15,
          label: '资源名称',
          clearValueOnEmpty: true,
          required: true,
        },
        {
          type: 'input-text',
          name: 'res_desc',
          label: '描述',
          showCounter: true,
          maxLength: 128,
          clearValueOnEmpty: true,
          required: true,
        },
      ],
    },
    actions: [
      {
        label: '取消',
        type: 'button',
        actionType: 'close',
        level: 'default',
      },
      {
        label: '确认',
        type: 'button',
        actionType: 'submit',
        level: 'primary',
      },
    ],
  },
}

let editResourceDialog = {
  label: '编辑',
  type: 'button',
  actionType: 'dialog',
  level: 'info',
  dialog: {
    title: '编辑资源',
    size: 'md',
    body: {
      type: 'form',
      api: {
        method: 'post',
        url: '/_dev/resource/edit',
        data: {
          '&': '$$',
          id: '${id}',
        },
      },
      body: [
        {
          type: 'select',
          name: 'system_id',
          label: '系统名称',
          clearValueOnEmpty: true,
          required: true,
          disabled: true,
          source: {
            method: 'get',
            url: '/_dev/system/select',
          },
        },
        {
          type: 'select',
          name: 'module_id',
          label: '模块名称',
          clearValueOnEmpty: true,
          required: true,
          disabled: true,
          required: true,
          source: '/_dev/module/select?system_id=${system_id}',
        },
        {
          type: 'input-text',
          name: 'res_code',
          maxLength: 15,
          label: '资源编号',
          clearValueOnEmpty: true,
          required: true,
        },
        {
          type: 'input-text',
          name: 'res_name',
          maxLength: 15,
          label: '资源名称',
          clearValueOnEmpty: true,
          required: true,
        },
        {
          type: 'input-text',
          name: 'res_desc',
          label: '描述',
          showCounter: true,
          maxLength: 128,
          clearValueOnEmpty: true,
          required: true,
        },
      ],
    },
    actions: [
      {
        label: '取消',
        type: 'button',
        actionType: 'close',
        level: 'default',
      },
      {
        label: '确认',
        type: 'button',
        actionType: 'submit',
        level: 'primary',
      },
    ],
  },
}

let delResourceButton = {
  label: '删除',
  type: 'button',
  actionType: 'ajax',
  level: 'danger',
  confirmText: '确认资源[${res_code}]?',
  api: {
    method: 'post',
    url: '/_dev/resource/del',
    data: {
      id: '${id}',
    },
  },
}

let resourceFilter = {
  title: '',
  body: [
    {
      type: 'group',
      body: [
        {
          type: 'select',
          name: 'system_id',
          placeholder: '系统名称',
          size: 'sm',
          clearable: true,
          source: '/_dev/system/select',
          onEvent: {
            change: {
              actions: [
                {
                  actionType: 'reload',
                  componentId: 'resource_table',
                },
              ],
            },
          },
        },
        {
          type: 'select',
          name: 'module_id',
          placeholder: '模块名称',
          size: 'sm',
          clearable: true,
          source: '',
          source: {
            method: 'get',
            url: '/_dev/module/select?system_id=${system_id}',
            sendOn: '${system_id != null}',
          },
        },
        {
          type: 'input-text',
          name: 'res_code',
          clearable: true,
          placeholder: '资源编号',
          size: 'sm',
        },
        {
          type: 'input-text',
          name: 'res_name',
          clearable: true,
          placeholder: '资源名称',
          size: 'sm',
        },
        {
          type: 'button-toolbar',
          buttons: [
            {
              type: 'reset',
              label: '重置',
            },
            {
              type: 'submit',
              level: 'primary',
              label: '查询',
            },
          ],
        },
      ],
    },
  ],
  actions: [],
}

let resourceTable = {
  type: 'crud',
  id: 'resource_table',
  autoFillHeight: true,
  syncLocation: false,
  perPage: 10,
  filter: resourceFilter,
  columnsTogglable:true,
  alwaysShowPagination: true,
  filterTogglable: true,
  headerToolbar: ['reload', 'bulkActions', 'filter-toggler', addResourceDialog],
  footerToolbar: [
    'statistics',
    {
      type: 'pagination',
      layout: 'perPage,pager,go',
    },
  ],
  tital: '',
  api: {
    method: 'get',
    url: '/_dev/resource/list',
    data: {
      system_id: '${system_id|default:undefined}',
      module_id: '${module_id|default:undefined}',
      res_code: '${res_code|default:undefined}',
      res_name: '${res_name|default:undefined}',
      page: '${page|default:undefined}',
      perPage: '${perPage|default:undefined}',
      orderBy: '${orderBy|default:undefined}',
    },
  },
  columns: [
    {
      name: 'sys_name',
      label: '系统名称',
    },
    {
      name: 'm_name',
      label: '模块名称',
      sortable: true,
    },
    {
      name: 'res_code',
      label: '资源编号',
      sortable: true,
    },
    {
      name: 'res_name',
      label: '资源名称',
      sortable: true,
    },
    {
      name: 'interface_count',
      label: '接口数',
    },
    {
      name: 'res_desc',
      label: '描述',
    },
    {
      type: 'operation',
      label: '操作',
      buttons: [editResourceDialog, delResourceButton],
      width: 150,
    },
  ],
}

export { resourceTable,addResourceDialog }

